﻿@using PagedList.Mvc
@using PagedList

@model IPagedList<ExcellOnServices.Models.Order>

@{
    ViewBag.Title = "Report";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
    decimal totalMoneyOrder = 0;
}

<div class="panel panel-primary">
    <div class="panel-heading">Report</div>
    <div class="panel-body">
        @using (Html.BeginForm("Report", "Orders", FormMethod.Post, new { @class = "form-inline" }))
        {
            <label for="status" style="display: inline-block">Filter Status</label>
            <div class="form-group">
                @Html.DropDownList("status", (IEnumerable<SelectListItem>)ViewBag.Status, "--Select Status--", new { @class = "form-control" })
            </div>
            <div class="pull-right">
                <div class="form-group">
                    @Html.TextBox("searchFromDate", null, new { @class = "form-control b-datetimepicker", @readonly = "readonly", @placeholder = "From Date" })
                </div>
                <div class="form-group">
                    @Html.TextBox("searchToDate", null, new { @class = "form-control b-datetimepicker", @readonly = "readonly", @placeholder = "To Date" })
                </div>
                <button type="submit" class="btn btn-primary">Search</button>
                <input type="reset" value="Clear" class="btn btn-default" />
            </div>
        }
    </div>
    @if (!Model.Any())
    {
        <div class="alert alert-info">
            No record were found.
            @Html.ActionLink("Back to show all orders", "Report", "Orders", null, new { @class = "alert-link" })
        </div>
    }
    else
    {
        <table class="table table-striped table-hover">
            <tr>
                <th>Company</th>
                <th>Begin Date</th>
                <th>End Date</th>
                <th>Payment</th>
                <th>Total of money</th>
                <th>Status</th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.OrderItems.FirstOrDefault().Product.Accounts_Company.BrandName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.BeginDateTime)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.EndDateTime)
                    </td>
                    <td>
                        @switch ((int)item.Payment)
                        {
                            case 0:
                                ViewBag.Payment = "Cash";
                                break;
                            case 1:
                                ViewBag.Payment = "Credit Card";
                                break;
                        }
                        @ViewBag.Payment
                    </td>
                    <td>
                        @{
                        decimal totalMoney = 0;
                        int countDate = item.EndDateTime.Subtract(item.BeginDateTime).Days;
                        foreach (var orItem in item.OrderItems)
                        {
                            totalMoney = (orItem.ServicePrice * countDate) + totalMoney;
                        }
                        totalMoneyOrder += totalMoney;
                        }
                        @String.Format("{0:C}", @totalMoney)
                    </td>
                    <td>
                        @switch ((int)item.Status)
                        {
                            case 0:
                                ViewBag.Status = "Pending";
                                break;
                            case 1:
                                ViewBag.Status = "Canceled";
                                break;
                            case 2:
                                ViewBag.Status = "Confirmed";
                                break;
                            case 3:
                                ViewBag.Status = "Activated";
                                break;
                            case 4:
                                ViewBag.Status = "Expired";
                                break;
                        }
                        @ViewBag.Status
                    </td>
                </tr>
            }
            <tr class="danger">
                <th colspan="4">
                    Total of money:
                </th>
                <td id="totalPrice">
                    @String.Format("{0:C}", @totalMoneyOrder)
                </td>
                <td></td>
            </tr>
        </table>
        <div class="panel-footer">
            <div class="pagedList" data-target="#left_content" style="text-align: center">
                @Html.PagedListPager(Model, page => Url.Action("Report", new { page, search = ViewBag.SearchString, status = ViewBag.StatusName }))
            </div>
        </div>
    }
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">
        $(function () {
            $(".b-datetimepicker").datepicker({ dateFormat: "dd/MM/yy" });
        });
    </script>
}

